<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ProvinceAgentCreateRequest;
use App\Http\Requests\ProvinceAgentUpdateRequest;
use App\Models\City;
use App\Models\ProvinceAgent;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ProvinceAgentController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
        return view('admin.provinceAgent.index');
    }
    public function data(Request $request)
    {
        $model = ProvinceAgent::query();
        if ($request->get('name')){
            $model = $model->where('name','like','%'.$request->get('name').'%');
        }
        if ($request->get('phone')){
            $model = $model->where('phone','like','%'.$request->get('phone').'%');
        }
        $res = $model->orderBy('created_at','desc')->paginate($request->get('limit',30))->toArray();
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $city = City::where(['level' => 1])->get()->toArray();
        return view('admin.provinceAgent.create', ['city' => $city]);
    }
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(ProvinceAgentCreateRequest $request)
    {
        $data = $request->all();
        $data['password'] = bcrypt($data['password']);
        $data['uuid'] = \Faker\Provider\Uuid::uuid();
        if (empty($data['province_id'])) {
            return redirect()->to(route('admin.provinceAgent'))->withErrors('系统错误');
        }
        if (ProvinceAgent::create($data)){
            return redirect()->to(route('admin.provinceAgent'))->with(['status'=>'添加账号成功']);
        }
        return redirect()->to(route('admin.provinceAgent'))->withErrors('系统错误');
    }
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {

        $city = City::where(['level' => 1])->get()->toArray();
        $provinceAgent = ProvinceAgent::findOrFail($id);

        return view('admin.provinceAgent.edit',[
            'provinceAgent' => $provinceAgent,
            'city' => $city
        ]);
    }
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(ProvinceAgentUpdateRequest $request, $id)
    {
        $member = ProvinceAgent::findOrFail($id);
        $data = $request->except('password');
        if ($request->get('password')){
            $data['password'] = bcrypt($request->get('password'));
        }
        if ($member->update($data)){
            return redirect()->to(route('admin.provinceAgent'))->with(['status'=>'更新用户成功']);
        }
        return redirect()->to(route('admin.provinceAgent'))->withErrors('系统错误');
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)){
            return response()->json(['code'=>1,'msg'=>'请选择删除项']);
        }
        if (ProvinceAgent::destroy($ids)){
            return response()->json(['code'=>0,'msg'=>'删除成功']);
        }
        return response()->json(['code'=>1,'msg'=>'删除失败']);
    }

    // 市代理收益


    public function getProvinceAgentIncomeLogData(Request $request)
    {
        $query =  \DB::table("province_agent_income_log as t1")
            ->leftJoin("province_agent as t2", "t1.province_agent_id", "=","t2.id")
            ->select("");
        if ($name = $request->input("name")){
                $query = $query->where("t2.name", "like", "%".$name."%");
        }

        if ($begin_time = $request->input("begin_time")){
            $query = $query->where("t1.created_at","like","%".$begin_time."%");
        }

        if ($end_time = $request->input("end_time")){
            $query = $query->where("t1.created_at", "like", "%".$end_time."%");
        }

        $res =  $query->orderBy("t1.created_at","desc")->paginate(10)->toArray();

        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $res['total'],
            'data'  => $res['data']
        ];
        return response()->json($data);

    }
}
